﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


#if NETFX_CORE || WINDOWS_PHONE
using Folder = Windows.Storage.StorageFolder;
#else
using Folder = System.IO.DirectoryInfo;
#endif



namespace WindowsCommonStorage
{

    public abstract class CommonStorageFolder:CommonStorageItem
    {

        public Folder EnvironmentFolder 
        {
            get 
            {
                return ErrorHandler.Func(() => 
                    {
                        if (this.ProtectedEnvironmentFolder == null) throw new ObjectDisposedException("Folder");
                        return ProtectedEnvironmentFolder; 
                    }); 
            } 
        }
        internal protected abstract Folder ProtectedEnvironmentFolder { get;}




        public async Task<IEnumerable<CommonStorageFile>> GetFilesAsync()
        {
            return await ProtectedGetFilesAsync().ConfigureAwait(false);
        }
        internal protected abstract Task<IEnumerable<CommonStorageFile>> ProtectedGetFilesAsync();

        public async Task<CommonStorageFolder> GetFolderAsync(string name)
        {
            return await ProtectedGetFolderAsync(name).ConfigureAwait(false);
        }
        internal protected abstract Task<CommonStorageFolder> ProtectedGetFolderAsync(string name);


        public async Task<IEnumerable<CommonStorageFolder>> GetFoldersAsync()
        {
            return await ProtectedGetFoldersAsync().ConfigureAwait(false);
        }
        internal protected abstract Task<IEnumerable<CommonStorageFolder>> ProtectedGetFoldersAsync();


        

        public async Task<CommonStorageFolder> CreateFolderAsync(string desiredName)
        {
            return await ErrorHandler.AsyncFunc(async() =>
            {
                return await ProtectedCreateFolderAsync(desiredName).ConfigureAwait(false) ;
            }).ConfigureAwait(false);
        }
        internal protected abstract Task<CommonStorageFolder> ProtectedCreateFolderAsync(string desiredName);


        public async Task<CommonStorageFolder> CreateFolderAsync(string desiredName, CommonCreationCollisionOption option)
        {
            return await ErrorHandler.AsyncFunc(async() =>
            {
                return await ProtectedCreateFolderAsync(desiredName, option).ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
        internal protected abstract Task<CommonStorageFolder> ProtectedCreateFolderAsync(string desiredName, CommonCreationCollisionOption option);

        public async Task<CommonStorageFile> CreateFileAsync(string desiredName)
        {
            return await ErrorHandler.AsyncFunc(async () =>
            {
                return await ProtectedCreateFileAsync(desiredName).ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
        internal protected abstract Task<CommonStorageFile> ProtectedCreateFileAsync(string desiredName);

        public async Task<CommonStorageFile> CreateFileAsync(string desiredName, CommonCreationCollisionOption option)
        {
            return await ErrorHandler.AsyncFunc(async () =>
            {
               return await ProtectedCreateFileAsync(desiredName, option).ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
        internal protected abstract Task<CommonStorageFile> ProtectedCreateFileAsync(string desiredName, CommonCreationCollisionOption option);


        public static async Task<CommonStorageFolder> GetFolderFromPathAsync(string path)
        {
            return await ErrorHandler.AsyncFunc(async () =>
            {
#if !(NETFX_CORE || WINDOWS_PHONE)
                return await NetFxStorageFolder.ProtectedGetFolderFromPathAsync(path).ConfigureAwait(false);
#else
                return await NetFxCoreStorageFolder.ProtectedGetFolderFromPathAsync(path).ConfigureAwait(false);
#endif
            }).ConfigureAwait(false);
        }
        

 
    }
}
